import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../pages/login.dart';
import '../widget/my_dialog.dart';
import '../services/user.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class SettingPage extends StatefulWidget {
  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  bool isLogin=false;
  Map<String, dynamic> userInfo; //用户信息

  @override
  void initState() {
    super.initState();
    getUserinfo();
  }

  //获取用户信息
  Future<void> getUserinfo() async {
    var getUserInfo = await User.getUserInfo();
    if (getUserInfo['code'] == 200) {
      setState(() {
        userInfo = getUserInfo['result'];
        isLogin = true;
      });
    } else {
      setState(() {
        userInfo = null;
        isLogin = false;
      });
    }
  }

  //退出登录
  Future<void> logout(context) async {
    MyDialog dialog = MyDialog();
    String result=await dialog.alertDialog(context);
    if(result=='ok'){
      await User.loginOut();
      User.loginOut();
      setState(() {
        isLogin = false;
      });
      Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(builder: (BuildContext context) => LoginPage()),
        ModalRoute.withName('/login'),
      );

    }
    if(result=='cancle'){
      Fluttertoast.showToast(msg: '取消成功');
    }
  }

  @override
  Widget build(BuildContext context) {
    if(isLogin){
      return Scaffold(
        appBar: AppBar(
          title: Text('设置'),
        ),
        body:
        Container(
          alignment: Alignment.center,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                width: 700.w,
                padding: EdgeInsets.all(20),
                child: RaisedButton(
                  color: Colors.red,
                  child: Text("退出登录",style: TextStyle(color: Colors.white),),
                  onPressed: () {
                    logout(context);
                  },
                ),
              )
            ],
          ),
        ),
      );
    }else{
      return Scaffold(
        body: Container(
          child: Text('未登陆'),
        ),
      );
    }

  }
}
